var optType = 1, optIndex = 0, optRow = null;
var config = {
    table:{
        title:'',
        toolbar:'#table-auth-btn',
        pagination: false,
        rownumbers: true,
        onDblClickRow:function () {
            optRow = component.datagrid.getRowData('#table-list');
            optIndex = component.datagrid.getDataRow('#table-list');
            action.col();
        },
        onClickRow:function () {
            optRow = component.datagrid.getRowData('#table-list');
            optIndex = component.datagrid.getDataRow('#table-list');
        },
        frozenColumns:[
            [
                {field:'table_name',title:'表名',width:200 }
            ]
        ],
        columns:[
            [
                {field:'engine',title:'类型', align:'center'},
                {field:'table_collation',title:'编码', align:'center'},
                {field:'table_rows',title:'总记录数', align:'center'},
                {field:'table_comment',title:'表注释', align:'left',
                    formatter:function (val, row, index) {
                        try{
                            return val.substring(0, val.length - 3);
                        }catch (e){
                            return val;
                        }
                    }
                },
                {field:'code',title:'表编号', align:'center'},
                {field:'create_time',title:'创表时间', align:'center'},
                {field:'update_time',title:'最后修改', align:'center'},
            ]
        ]
    },
    col:{
        title:'',
        pagination: false,
        rownumbers: true,
        columns:[
            [
                {field:'column_name',title:'字段', align:'left', width:150},
                {field:'column_type',title:'类型', align:'left',width:180},
                {field:'column_comment',title:'注释', align:'left',width:280},
            ]
        ]
    },
    label: {
        width:600,
        height:400,
        left: (kit.wh().width - 600) / 2
    },
}
$(function () {
    action.get();
})

var opt = {
    generator:function () {
        if(optRow == null){
            kit.alert('请先选择一个数据表', 'warning');
            return false;
        }
        action.generator();
    }
}
var action = {
    get: function () {
        var param = {
            url: constant.route.generator
        };
        var request = http.get(param);
        request.done(function (d) {
            config.table.data = d.result;
            component.datagrid.init('#table-list', config.table);
        })
    },
    col: function () {
        var param = {
            url: constant.route.generator + '/' + optRow.table_name
        };
        var request = http.get(param);
        request.done(function (d) {
            var data = d.result;
            var name = optRow.table_comment;
            config.label.title = name.substring(0, name.length - 3);
            var _height = data.length * 60;
            config.label.height = _height > 400 ? 400 : _height;
            component.dialog.init('#opt-form-col', config.label);
            component.dialog.open('#opt-form-col');
            config.col.data = data;
            component.datagrid.init('#table-list-col', config.col);
        })
    },
    generator: function () {
        var data = {
            code: optRow.code,
            tableName: optRow.table_name
        }
        var param = {
            url: constant.route.generator,
            data: data
        };
        var request = http.post(param);
        request.done(function (d) {
            kit.alert(d.message, 'success');
        })
    }
}